#include "ImageConverter.hpp"
#include <DoQuMeasures/Exceptions.hpp>
#include <QPixmap>

ImageConverter::ImageConverter()
{
}


girl::tools::Image ImageConverter::convert(const QImage &pImage)
{

    QPixmap pixmap = QPixmap::fromImage(pImage);
    QImage image = pixmap.toImage();

    const int width = image.width();
    const int height = image.height();

    girl::tools::Image  gtImg(width, height);



    if(gtImg.width() != width || gtImg.height() != height)
    {
        throw Exceptions::NotSameSizeException(gtImg,gtImg);
    }

    for (int j=0; j<height; ++j) {
        QRgb *line = (QRgb*)image.scanLine(j);
        unsigned char *lBuf = gtImg.scanLine(j);
        for (int i=0; i<width; ++i) {
            lBuf[0] = qRed(*line);
            lBuf[1] = qGreen(*line);
            lBuf[2] = qBlue(*line);

            lBuf += gtImg.formatSize();
            ++line;
        }
    }

    return gtImg;

}


QImage ImageConverter::convert(const girl::tools::Image &image)
{
    QImage out(image.width(),image.height(),QImage::Format_RGB32);
    for (int j=0; j<image.height(); ++j) {
        for (int i=0; i<image.width(); ++i) {
            girl::tools::Pixel p = image.getPixel(i,j);
            out.setPixel(i,j,qRgb(p.a(),p.b(),p.c()));
        }
    }
    return out;
}
